﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using Billing.IService.Entry;
using Pay.Models;
using Framework.MVC;
using Framework.Common.Event;
using Pay.Entry;
using Log.Model;
using Framework.MVVM.Model;
using Log.IService;
using Log.Service;
using Common.IService;
using Common.Service;

namespace Pay.Controllers
{
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class PayController : AsyncControllerBase, IWriteLog
    {
        #region Filed

        private static readonly string m_ip = ConfigurationManager.AppSettings["ip"];
        private static readonly string m_mode = ConfigurationManager.AppSettings["mode"];

        private const char m_split = ';';
        private const string m_access_error = "access001";

        #endregion Filed

        #region New

        [AcceptVerbs(HttpVerbs.Post)]
        public void NewAsync(PayOrderModel model)
        {
            string[] ip = m_ip.Split(m_split);
            if (!ip.Contains(Request.UserHostAddress))
            {
                this.Log(m_access_error, Request.UserHostAddress);
                return;
            }
            model.Source = Request.UserHostAddress;
            InitModel(model);
        }

        public ActionResult NewCompleted(PayCenterOrderEntry entry)
        {
            return entry == null ? null : Json(entry);
        }

        #endregion New

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest(this, EventArgs.Empty);
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest(this, EventArgs.Empty);
        }

        #endregion IWriteLog
    }
}
